package logic

import (
	"context"
	"src/models/mysql"
	"src/srcclient"

	"src/internal/svc"
	"src/src"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *src.GoodsListRequest) (*src.GoodsListResponse, error) {
	// todo: add your logic here and delete this line

	var good []mysql.Good
	mysql.DB.Debug().Model(&good).Where("good_title   like ?", "%"+in.GoodTitle+"%").Order("like_collect  desc").Find(&good)

	var slice []*srcclient.GoodsListStruct

	for _, V := range good {
		list := srcclient.GoodsListStruct{
			GoodTitle:   V.GoodTitle,
			GoodPrice:   int64(V.GoodPrice),
			GoodStock:   int64(V.GoodStock),
			GoodStatus:  int64(V.GoodStatus),
			LikeCollect: int64(V.LikeCollect),
		}
		slice = append(slice, &list)
	}
	return &src.GoodsListResponse{List: slice}, nil
}
